﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Xml;

namespace StdDoc
{
    public class Convert
    {
        public static List<DataSet> ToDataSetFromFile(string path)
        {
            try
            {
                List<DataSet> results = new List<DataSet>();
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(path);

                //find section
                XmlNodeList nodes = xDoc.SelectNodes("/Document/Data/Section");
                //nodes is a list of section.
                //loop section
                foreach (XmlNode sectionNode in nodes)
                {
                    //new dataset for section.
                    DataSet result = new DataSet(sectionNode.Attributes["ID"].Value);

                    //find items.
                    XmlNodeList itemsSectionNode = sectionNode.SelectNodes("Items");
                    foreach (XmlNode itemNodes in itemsSectionNode)
                    {
                        Dictionary<string, string> listColumn = new Dictionary<string, string>();
                        //Create new data table for items.
                        DataTable myTable = new DataTable(itemNodes.Attributes["ID"].Value);

                        //find meaning of section and set it as header.
                        XmlNodeList meanList = itemNodes.SelectNodes("Means/Mean");
                        foreach (XmlNode meanNode in meanList)
                        {
                            DataColumn colData = new DataColumn(meanNode.Attributes["Name"].Value);
                            listColumn.Add(meanNode.Attributes["ID"].Value, meanNode.Attributes["Name"].Value);
                            myTable.Columns.Add(colData);
                        }

                        //find item and fill to datatable.
                        XmlNodeList itemList = itemNodes.SelectNodes("Item");
                        foreach (XmlNode itemNode in itemList)
                        {
                            DataRow dr = myTable.NewRow();

                            //select attributes
                            foreach (XmlAttribute attrib in itemNode.Attributes)
                            {
                                dr[listColumn[attrib.Name]] = attrib.Value;
                            }
                            myTable.Rows.Add(dr);
                        }


                        //Add datatable to dataset.
                        result.Tables.Add(myTable);
                    }
                    results.Add(result);
                }

                return results;
            }
            catch
            {
                throw new Exception("You standard document is not valid format.");
            }
        }
        public static List<DataSet> ToDataSet(string docs)
        {
            try
            {
                List<DataSet> results = new List<DataSet>();
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(docs);

                //find section
                XmlNodeList nodes = xDoc.SelectNodes("/Document/Data/Section");
                //nodes is a list of section.
                //loop section
                foreach (XmlNode sectionNode in nodes)
                {
                    //new dataset for section.
                    DataSet result = new DataSet(sectionNode.Attributes["ID"].Value);

                    //find items.
                    XmlNodeList itemsSectionNode = sectionNode.SelectNodes("Items");
                    foreach (XmlNode itemNodes in itemsSectionNode)
                    {
                        Dictionary<string, string> listColumn = new Dictionary<string, string>();
                        //Create new data table for items.
                        DataTable myTable = new DataTable(itemNodes.Attributes["ID"].Value);

                        //find meaning of section and set it as header.
                        XmlNodeList meanList = itemNodes.SelectNodes("Means/Mean");
                        foreach (XmlNode meanNode in meanList)
                        {
                            DataColumn colData = new DataColumn(meanNode.Attributes["Name"].Value);
                            listColumn.Add(meanNode.Attributes["ID"].Value, meanNode.Attributes["Name"].Value);
                            myTable.Columns.Add(colData);
                        }

                        //find item and fill to datatable.
                        XmlNodeList itemList = itemNodes.SelectNodes("Item");
                        foreach (XmlNode itemNode in itemList)
                        {
                            DataRow dr = myTable.NewRow();

                            //select attributes
                            foreach (XmlAttribute attrib in itemNode.Attributes)
                            {
                                dr[listColumn[attrib.Name]] = attrib.Value;
                            }
                            myTable.Rows.Add(dr);
                        }


                        //Add datatable to dataset.
                        result.Tables.Add(myTable);
                    }
                    results.Add(result);
                }

                return results;
            }
            catch
            {
                throw new Exception("You standard document is not valid format."); 
            }
        }
        public static List<DataSet> ToDataSet(XmlDocument xDoc)
        {
            try
            {
                List<DataSet> results = new List<DataSet>();

                //find section
                XmlNodeList nodes = xDoc.SelectNodes("/Document/Data/Section");
                //nodes is a list of section.
                //loop section
                foreach (XmlNode sectionNode in nodes)
                {
                    //new dataset for section.
                    DataSet result = new DataSet(sectionNode.Attributes["ID"].Value);

                    //find items.
                    XmlNodeList itemsSectionNode = sectionNode.SelectNodes("Items");
                    foreach (XmlNode itemNodes in itemsSectionNode)
                    {
                        Dictionary<string, string> listColumn = new Dictionary<string, string>();
                        //Create new data table for items.
                        DataTable myTable = new DataTable(itemNodes.Attributes["ID"].Value);

                        //find meaning of section and set it as header.
                        XmlNodeList meanList = itemNodes.SelectNodes("Means/Mean");
                        foreach (XmlNode meanNode in meanList)
                        {
                            DataColumn colData = new DataColumn(meanNode.Attributes["Name"].Value);
                            listColumn.Add(meanNode.Attributes["ID"].Value, meanNode.Attributes["Name"].Value);
                            myTable.Columns.Add(colData);
                        }

                        //find item and fill to datatable.
                        XmlNodeList itemList = itemNodes.SelectNodes("Item");
                        foreach (XmlNode itemNode in itemList)
                        {
                            DataRow dr = myTable.NewRow();

                            //select attributes
                            foreach (XmlAttribute attrib in itemNode.Attributes)
                            {
                                dr[listColumn[attrib.Name]] = attrib.Value;
                            }
                            myTable.Rows.Add(dr);
                        }


                        //Add datatable to dataset.
                        result.Tables.Add(myTable);
                    }
                    results.Add(result);
                }

                return results;
            }
            catch
            {
                throw new Exception("You standard document is not valid format."); 
            }
        }

    }
}
